mui.init({
});
var old_back = mui.back;
mui.back = function(){
	//显示上一个界面；
var page=mui.currentWebview.opener();

page.show();
mui.fire(page,'reload',{});
//继续当前页面原有返回逻辑
old_back();
}

var IdMgr = {
	
	btnischoose :'ischoose_',
	
	delbank :'delbank_',
	/*修改银行ID*/
	editBank :'editBank_'
};
mui.plusReady(function() {
	/**
	 * 当点击首页是调用“刷新”事件 
	 */
	window.addEventListener('reload',function(event){
		loadDatas();
	});
	

	function loadDatas(){
		var userId = plus.storage.getItem(KeyCfg.userId);
		var pars = {userId:userId};
		var url = UrlConfig.mybank_load;
		MuiEventMgr.ajax(url,{pars:pars,
		  	sfn : function(data){
		  		var pars = null;
		  		pars=setVals(data);
		  		ischoose(pars);
		  		delbank(pars);
		  		editbankListeners(pars);
		  	},ffn : function(err){
		  		mui.alert("获取数据失败：["+err+"]");
		  	}
		 });
	}
	
	//加载数据
	loadDatas();

/*	(function(){		
		var m=CmwMcore.$("add_record");
		MeventMgr.onTouchstart(m,function(event,self){
			var userId = plus.storage.getItem(KeyCfg.userId);
			var self=plus.webview.currentWebview();
			plus.storage.setItem(KeyCfg.targetPageId, self.id);
			var name = self.realName;
			openPage('../mybank/addmybank.html',{userId:userId,realName:name});
		});
	})();*/


	/**
	 * 打开新页面 
 	 * @param {Object} url	新地址
	 * @param {Object} pars	参数
	 */
	function openPage(url ,pars){
		if(!pars) pars = {};
		var cfg = {pars : pars};
		MuiEventMgr.openBySafe(url,cfg);
	}

	function setVals(data){
		var lists = data.bankLists;
		var bankTypes = data.bankType;
		var htmlArr = [];
		var eleIds = [];
		var delIds =[];
		var editIds = [];
		if(!lists || lists.length == 0){
			$("#bank_info").html(" "); 
			return;
		}
		for(var i=0;i<lists.length;i++){
			var userbank = lists[i];
			htmlArr[i] = getJbHtm(delIds,eleIds,editIds,userbank,bankTypes);
		}
		var htmlStr = htmlArr.join(" ");
		$("#bank_info").html(htmlStr);
		var pars ={eleIds:eleIds,delIds:delIds,editIds:editIds}
		return pars;
	}
	
	function getJbHtm(delIds,eleIds,editIds,userbank,bankTypes){
			var bankcardtypeId= userbank.bankcardtype;
			var bankName=userbank.bankName;
			var username=userbank.username;
			var openBank=userbank.openBank;
			var bankAcconut=userbank.bankAccount;
			var appid = userbank.appId;
			var isChoose = userbank.isChoose;
			var guid = userbank.guid;
			var btnId = IdMgr.btnischoose+guid;
			var delId = IdMgr.delbank+guid;
			var editId = IdMgr.editBank+guid;
			delIds[delIds.length]=delId;
			eleIds[eleIds.length] = btnId;
			editIds[editIds.length] = editId;
			var style ="background-color:#999999";
			var htmlArr = "";
			var choose="绑定";
			var booler="isChooe_true";
//			var statusHtml = '<a style="padding: 10px 33px;color: #0b56a7;" class="record_update" href="javascript:void(0)" id="'+editId+'">修改</a>';
			var delHtml = '<a style="line-height: 30px;" href="javascript:void(0)" id="'+delId+'" class="mhd_mb_form_button button_gray">删除</a>';
			if(isChoose == 1){ 
				style ="background-color:#006600";
				booler ="isChooe_false";
				choose="取消绑定";
			var	statusHtml = '<a style="padding: 10px 33px;color: #009315;" class="record_update">√&nbsp;已绑定</a>';
				delHtml = '';
			}
			htmlArr=[
				
			'<ul class="mui-table-view">',
	       	'   <li style="padding: 5px 15px;" class="mui-table-view-cell">',
	       	'    	<i style="margin-top: -4px;" class="bank-small banksmall-'+appid+'"></i>',
	       	'    	<span class="bank-name">'+bankName+'</span>',
	       	        statusHtml,
	       	'   </li>',
			'	<li style="padding: 5px 15px;" class="mui-table-view-cell">',
			'		<span class="record_left">尾号:</span>',
			'		<span class="record_right">'+bankAcconut+'</span>',
			'	</li>',
			'	<li style="padding: 5px 15px;" class="mui-table-view-cell">',
			'	    <span class="record_left">户名:</span>',
			'		<span class="record_right">'+username+'</span>',
			'	</li>',
			'	<li style="padding: 5px 15px;" class="mui-table-view-cell">',
			'	    <span class="record_left">开户行:</span>',
			'		<span class="record_right">'+openBank+'</span>',
			'	</li>',
			'	<div class="mhd_mb_form_bottom">',
			'	 	<a style="line-height: 30px;" href="javascript:void(0)" id="'+btnId+'" class="mhd_mb_form_button button_blue">'+choose+'</a>', 
			        delHtml,
			'	 </div>',
	        '</ul>'
				
				];
		return htmlArr.join(" ");
		
	}

	(function(){
		/**
		 * 添加银行卡
		 */
		var m=CmwMcore.$("add_record");
		MeventMgr.onTouchstart(m,function(event,self){
			var targetPageId = plus.webview.currentWebview().id;
			plus.storage.setItem(KeyCfg.targetPageId,targetPageId);
			openPage('../mybank/addmybank.html',{});
		});
	})();
	/**
	 * 绑定\解绑 银行卡
	 * @param {Object} pars
	 */
	function ischoose(pars){
		if(!pars) return;
		var eleIds = pars.eleIds; 
		if(!eleIds || eleIds.length == 0) return;
		for(var i=0,count=eleIds.length; i<count; i++){
			var eleId = eleIds[i];
			var ele = CmwMcore.$(eleId);
			var success=["确定","取消"];
			MeventMgr.onTouchstart(ele,function(event,self){
				var eleId = self.id;
				var name = document.getElementById(eleId).innerHTML;					
				var data = {}; 
				var id = eleId.replace(IdMgr.btnischoose,"");
				var url = UrlConfig.mybank_isChoose;
				var message = "你确定要绑定此卡？";
				data = {guid:id};
				if(name == "取消绑定"){
					url =UrlConfig.mybank_cancel;
					message = "你确定要解除绑定此卡？";
				}
				mui.confirm(message,"提示",success,function(e){
					if(e.index != 0)return;					
					MuiEventMgr.ajax(url,{pars:data,
					  	sfn : function(data){
					  		mui.alert(data.msg);
					  		//loadDatas();
					  		window.location.reload();
					  	},ffn : function(err){
					  		mui.alert(data.msg);
					  	}
			 		});
				});
				
			});
		}
	}
	
	
	
	
	/**
	 * 删除银行卡
	 * @param {Object} pars
	 */
	function delbank(pars){
		if(!pars) return;
		var delIds = pars.delIds;
		if(!delIds || delIds.length == 0) return;
		for(var i=0,count=delIds.length; i<count; i++){
			var eleId = delIds[i];
			var ele = CmwMcore.$(eleId);
			var success=["确定","取消"];
			MeventMgr.onTouchstart(ele,function(event,self){
				mui.confirm("你确定要删除此卡？","提示",success,function(e){
					if(e.index != 0)return;					
					var eleId = self.id;
					var data = {};
					var id = eleId.replace(IdMgr.delbank,"");
					var url = UrlConfig.mybank_bankDel;
					data = {guid:id};
					MuiEventMgr.ajax(url,{pars:data,
					  	sfn : function(data){
					  		mui.alert(data.msg);
					  		loadDatas();
					  	},ffn : function(err){
					  		mui.alert(data.msg);
					  	}
			 		});
				});
				
			});
		}
	}
	
	/**
	 * 修改银行卡	
	 * @param {Object} pars
	 */
	function editbankListeners(pars){
		if(!pars) return;
		var editIds = pars.editIds;
		if(!editIds || editIds.length == 0) return;
		for(var i=0,count=editIds.length; i<count; i++){
			var eleId = editIds[i];
			var ele = CmwMcore.$(eleId); 
			var success=["确定","取消"];
			MeventMgr.onTouchstart(ele,function(event,self){
				mui.confirm("你确定要修改此卡？","提示",success,function(e){
					if(e.index != 0) return;					
					var eleId = self.id;
					var guid = eleId.replace(IdMgr.editBank,"");
					pars = {guid:guid};
					var cfg = {pars :pars};
					MuiEventMgr.open("../mybank/editmybank.html",cfg);	
				});
			});
		}
	}
});
